package com.carkeeper.order.service.impl;

import com.carkeeper.order.dao.OrderMapper;
import com.carkeeper.order.pojo.Order;
import com.carkeeper.order.service.OrderService;
import com.carkeeper.order.service.OutstandingService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;
@Service
public class OutstandingServiceImpl implements OutstandingService {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderService orderService;
    //分页查询
    @Override
    public Page<Order> findPage(int page, int size) {
        PageHelper.startPage(page,size);
        Example example = new Example(Order.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andNotEqualTo("orderStatus",2).andNotEqualTo("orderStatus",1).andNotEqualTo("orderStatus",5);
        return (Page<Order>) orderMapper.selectByExample(example);

    }

    @Override
    public Page<Order> findPage(Map<String, Object> searchMap, int page, int size) {
       PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        return (Page<Order>) orderMapper.selectByExample(example);
    }

    //修改状态
    @Override
    public void closeOrder(String orderId) {
        Order order = orderService.findByid(orderId);
        order.setOrderStatus('5');
        System.out.println(order.getId());
        orderMapper.updateByPrimaryKeySelective(order);
    }*/

    //获取所有未接单的订单
    @Override
    public List<Order> findByStatus() {
        Example example = new Example(Order.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("orderStatus",3);
       return(List<Order>)orderMapper.selectByExample(example);
    }

    /**
     * 构建查询对象
     *
     * @param searchMap
     * @return
     */
    public Example createExample(Map<String,Object> searchMap){
        Example example = new Example(Order.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap!=null){
            // 订单编号
            if (searchMap.get("code")!=null &&!"".equals(searchMap.get("code"))){
                criteria.andEqualTo("code",searchMap.get("code"));
            }
            //订单时间
            if (searchMap.get("createDate")!=null&&!"".equals(searchMap.get("createDate"))){
                criteria.andEqualTo("createDate",searchMap.get("createDate"));
            }
            //客户名字
            if (searchMap.get("contact")!=null &&!"".equals(searchMap.get("contact"))){
                criteria.andEqualTo("contact",searchMap.get("contact"));
            }
            //店铺名称
            if (searchMap.get("storeName")!=null&&!"".equals(searchMap.get("storeName"))){
                criteria.andEqualTo("storeName",searchMap.get("storeName"));
            }
            criteria.andNotEqualTo("orderStatus",2).andNotEqualTo("orderStatus",1).andNotEqualTo("orderStatus",5);
        }
        return example;
    }
}
